package com.wzh.websocket.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.messaging.simp.config.MessageBrokerRegistry;
import org.springframework.web.socket.config.annotation.EnableWebSocketMessageBroker;
import org.springframework.web.socket.config.annotation.StompEndpointRegistry;
import org.springframework.web.socket.config.annotation.WebSocketMessageBrokerConfigurer;

/**
 * @Author zh wang
 * @Create 2025/5/16 16:07
 */
@Configuration
@EnableWebSocketMessageBroker
public class WebSocketConfig implements WebSocketMessageBrokerConfigurer {

    @Override
    public void registerStompEndpoints(StompEndpointRegistry registry) {
        registry.addEndpoint("/ws")
                //.setHandshakeHandler(new UserHandshakeInterceptor()) // 关键：注册拦截器
                .setAllowedOriginPatterns("http://localhost:5173/");
                //.withSockJS();
    }

    @Override
    public void configureMessageBroker(MessageBrokerRegistry registry) {
        // 启用简单消息代理，客户端可订阅/topic前缀的通道接收服务器广播消息。
        // 启用用户专属队列
        registry.enableSimpleBroker("/topic","/user");
        // 设置应用目的地前缀，客户端发送到/app前缀的消息会被路由到对应的消息处理方法。
        registry.setApplicationDestinationPrefixes("/app");
        registry.setUserDestinationPrefix("/user"); // 用户路由前缀
    }

}